package com.tiaotiao.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

public class solrTest {

	@Test
	public void select() throws SolrServerException, IOException {
		String url = "http://192.168.11.174:8983/solr/gettingstarted";
		HttpSolrClient server = new HttpSolrClient(url);
	    SolrQuery query = new SolrQuery();
	    query.setRequestHandler("/query");
	    // 以下给两个字段开启了高亮，分别是name，description，
	    query.setQuery("宝安");
	    query.setParam("start", "0");
	    query.setParam("rows", "20");
	    query.setHighlight(true);
	    query.addHighlightField("merchantName");
	    query.addHighlightField("mobileUsername");
	    query.addHighlightField("merchantShortName");
	    query.addHighlightField("address");
	    // 以下两个方法主要是在高亮的关键字前后加上html代码
	    query.setHighlightSimplePre("<font color=\"red\">");
	    query.setHighlightSimplePost("</font>");
	    QueryResponse response = server.query(query);
	    Map<String, Map<String, List<String>>> map = response.getHighlighting();
	    SolrDocumentList docs = response.getResults();
	    System.out.println("文档个数：" + docs.getNumFound());
	    System.out.println("查询时间：" + response.getQTime());
	    for (SolrDocument doc : docs) {
	        System.out.println("id: " + doc.getFieldValue("id"));
	        System.out.println("merchantName: " + doc.getFieldValue("merchantName"));
	        System.out.println("merchantShortName: " + doc.getFieldValue("merchantShortName"));
	        System.out.println("mobileUsername: " + doc.getFieldValue("mobileUsername"));
	        System.out.println("address: " + doc.getFieldValue("address"));
	        System.out.println(map);
	    }

	}
	@Test
	public void solrAdd() throws SolrServerException, IOException{
		String urlString = "http://192.168.11.174:8983/solr/gettingstarted";
		SolrClient solr = new HttpSolrClient(urlString);
		SolrInputDocument document = new SolrInputDocument();
		document.addField("id", "520520");
		document.addField("merchantName", "zouruijin merchantName1");
		document.addField("merchantShortName", "zouruijin merchantShortName1");
		document.addField("mobileUsername", "zouruijin mobileUsername1");
		document.addField("address", "zouruijin address1");
		UpdateResponse response = solr.add(document);
		 
		// Remember to commit your changes!
		 
		solr.commit();
	}
	@Test
	public void solrDel() throws SolrServerException, IOException{
		String urlString = "http://192.168.11.174:8983/solr/gettingstarted";
		SolrClient solr = new HttpSolrClient(urlString);
		solr.deleteById("520520");
		// Remember to commit your changes!
		 
		solr.commit();
	}
}

